package tech.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.aistar.entity.trading.Goods;
import tech.aistar.entity.trading.GoodsType;
import tech.aistar.service.IGoodsService;
import tech.aistar.service.IGoodsTypeService;
import tech.aistar.util.QiniuUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ace
 * @version 1.0
 * @descriiption:本类用来演示:
 * @date 2019/9/28 0028.
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsTypeService goodsTypeService;
    @Autowired
    private QiniuUtil qiniuUtil;
    @RequestMapping("/trading_homepage")
    public String homepage(Model model){
        //转发到二手交易首页jsp
        //调用service层方法
        List<GoodsType> goodsTypeList=goodsTypeService.findAll();
        model.addAttribute("goodsTypeList",goodsTypeList);
        return "trading/homepage";
    }
//四种商品类型的单独异步分页
//    食物
    @RequestMapping("/list")
    public String list(Model model,@RequestParam(defaultValue="") String name, @RequestParam(defaultValue = "1") Integer goodstypeid,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "6") Integer pageSize){
        // 数据的回显
        model.addAttribute("name",name);
        //调用service - 分页
        PageInfo<Goods> pageInfo = goodsService.findAll2(name,Integer.valueOf(goodstypeid),Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo.getPages());

        List<Goods> goodsList= goodsService.findAllLike(name,null);

        model.addAttribute("goodsList",goodsList);

        return "trading/pagedate1";
    }
//玩具
    @RequestMapping("/list2")
    public String list2(Model model,@RequestParam(defaultValue="") String name, @RequestParam(defaultValue = "2") Integer goodstypeid,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "6") Integer pageSize){
        // 数据的回显
        model.addAttribute("name",name);
        //调用service - 分页
        PageInfo<Goods> pageInfo = goodsService.findAll2(name,Integer.valueOf(goodstypeid),Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo.getPages());

        List<Goods> goodsList= goodsService.findAllLike(name,null);

        model.addAttribute("goodsList",goodsList);
        System.out.println("====="+goodstypeid);
        return "trading/pagedate2";
    }

//服饰
@RequestMapping("/list3")
public String list3(Model model,@RequestParam(defaultValue="") String name, @RequestParam(defaultValue = "3") Integer goodstypeid,
                     @RequestParam(defaultValue = "1") Integer pageNum,
                     @RequestParam(defaultValue = "6") Integer pageSize){
    // 数据的回显
    model.addAttribute("name",name);
    //调用service - 分页
    PageInfo<Goods> pageInfo = goodsService.findAll2(name,Integer.valueOf(goodstypeid),Integer.valueOf(pageNum),Integer.valueOf(pageSize));
    model.addAttribute("pageInfo",pageInfo);
    System.out.println(pageInfo.getPages());

    List<Goods> goodsList= goodsService.findAllLike(name,null);

    model.addAttribute("goodsList",goodsList);
    System.out.println("====="+goodstypeid);
    return "trading/pagedate3";
}

//其他
@RequestMapping("/list4")
public String list4(Model model,@RequestParam(defaultValue="") String name, @RequestParam(defaultValue = "4") Integer goodstypeid,
                     @RequestParam(defaultValue = "1") Integer pageNum,
                     @RequestParam(defaultValue = "6") Integer pageSize){
    // 数据的回显
    model.addAttribute("name",name);
    //调用service - 分页
    PageInfo<Goods> pageInfo = goodsService.findAll2(name,Integer.valueOf(goodstypeid),Integer.valueOf(pageNum),Integer.valueOf(pageSize));
    model.addAttribute("pageInfo",pageInfo);
    System.out.println(pageInfo.getPages());

    List<Goods> goodsList= goodsService.findAllLike(name,null);

    model.addAttribute("goodsList",goodsList);
    System.out.println("====="+goodstypeid);
    return "trading/pagedate4";
}
//分页结束
    @RequestMapping("/list1")
    @ResponseBody
    public List<Goods> list1(){
        System.out.println("===================================================================jj");
        return goodsService.findAll1();
    }

    /**
     * 添加商品
     */
    @PostMapping("/add")
    public String add(Model model, Integer userId,String goodsTypeId,
                      String goodsName,String degree,Double price,String goodsDetails,
                      String province,String city,String district,MultipartFile goodsPicture){
        Map<String,String> map = new HashMap<>();
        String imgUrl = null;
        try {
            imgUrl = qiniuUtil.upload(goodsPicture.getBytes(),goodsPicture.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("imgUrl", imgUrl);
        System.out.println(imgUrl);

        //调用service层方法
        List<GoodsType> goodsTypeList=goodsTypeService.findAll();
        model.addAttribute("goodsTypeList",goodsTypeList);
        String str=province+"-"+city+"-"+district;
        Goods g=new Goods();
        g.setUserId(userId);
        g.setGoodsName(goodsName);
        g.setGoodsTypeId(goodsTypeId);
        g.setDegree(degree);
        g.setPrice(price);
        g.setGoodsDetails(goodsDetails);
        g.setGoodsPicture(map.get("imgUrl"));
        g.setPlace(str);
        goodsService.save(g);
        return "redirect:/goods/trading_homepage";
    }
    /**
     * 删除商品
     */
    @RequestMapping("/del")
    public String del(HttpServletRequest req,HttpServletResponse resp){
        String[] ids=req.getParameterValues("id");
            for (String s: ids){
                goodsService.delById(Integer.valueOf(s));
            }
        return "redirect:/goods/trading_homepage";
    }
    /**
     * 修改商品
     */
    @SuppressWarnings("all")
    @PostMapping("/update")
    public String update(Model model,String id,String goodsTypeId,
                         String goodsName,String degree,Double price,String goodsDetails,
                         String province,String city,String district,
                         MultipartFile goodsPicture){
        Map<String,String> map = new HashMap<>();
        String imgUrl = null;
        try {
            imgUrl = qiniuUtil.upload(goodsPicture.getBytes(),goodsPicture.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("imgUrl", imgUrl);
            System.out.println(imgUrl);

        String str=province+"-"+city+"-"+district;
        Goods g=goodsService.findById(Integer.valueOf(id));
        g.setGoodsName(goodsName);
        g.setGoodsTypeId(goodsTypeId);
        g.setDegree(degree);
        g.setPrice(price);
        g.setGoodsDetails(goodsDetails);
        g.setGoodsPicture(map.get("imgUrl"));
        g.setPlace(str);
        //调用service层方法
        System.out.println(g);
        goodsService.Update(g);
        return "redirect:/goods/trading_homepage";
//        return "ok";
    }
}
